package com.asistencia.DAO.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Connector {
    private static Connection conx = null;
    private static final String svr = "localhost";
    private static final String bd = "reloj";
    private static final String usr = "root";
    private static final String pwd = "";

    public static void openConnection(){
        /*JOptionPane.showMessageDialog(null, conx);*/
        if(conx == null){
            try {
                Class.forName("com.mysql.jdbc.Driver");

                conx = DriverManager.getConnection("jdbc:mysql://" + svr + "/" + bd + "?user=" + usr + "&password=" + pwd);
            } catch (ClassNotFoundException ex) {
                /*JOptionPane.showMessageDialog(null,ex);*/
                Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                /*JOptionPane.showMessageDialog(null,ex);*/
                Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static Statement getStatement(){
        Statement stmnt = null;
        try {
            stmnt = conx.createStatement();
        } catch (SQLException ex) {
            /*JOptionPane.showMessageDialog(null,ex);*/
            Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return stmnt;
        }
    }
    public static void closeConnection(){
        try {
            if(!conx.isClosed())
            {conx.close();}
        } catch (SQLException ex) {
            /*JOptionPane.showMessageDialog(null,ex);*/
            Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
